package buber.genetic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import buber.config.Config;
import buber.genetic.interfaces.Member;
import buber.genetic.interfaces.Population;



public class SimplePopulation implements Population {
	private int chSize=20;
	private int chCount=1;
	private List<Member> members;

	public SimplePopulation(){
		members = new ArrayList<Member>();
	}
	
	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> members) {
		this.members = members;
	}

	public String asString(){
		StringBuffer sb = new StringBuffer();
		for(Iterator<Member> pit = members.iterator();pit.hasNext();){
			sb.append(pit.next().asString());
			sb.append("\n");
		}
		return sb.toString();
	}

	public Member getRandomMemeber(){
		Member member = new SimpleMember();
		member.setChromosomeCount(chCount);
		member.setChromosomeSize(chSize);
		member.init();
		return member;
	}
	public void addMember(Member m){
		members.add(m);
	}
	public int size(){
		return members.size();
	}

	public void trimTo(int howMany) {
		for(int i=members.size()-1;i>=howMany;i--){
			members.remove(i);
		}
		
	}
	public Population getClone(){
		Population p = new SimplePopulation();
		for(Iterator<Member> mit=members.iterator();mit.hasNext();){
			p.addMember(((Member)(mit.next())).getClone());
		}
		return p;
	}

	public Population joinWith(Population pop) {
		members.addAll(pop.getMembers());
		return this;
	}
	public void genocide(){
		members.clear();
	}

	public void config(Config cfg) {
		chSize=cfg.getChSize();
		chCount=cfg.getChromosomes();
	}
}
